home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / Programming / Algorithms Folder / upload / prolog.dat.readme < prev    next >
Encoding:
Text File  |  1994-05-18  |  3.7 KB  |  173 lines  |  [TEXT/ttxt]

  1. TO USE THESE PROGRAMS FIRST CUT THEM OUT TO SEPARATE FILES
  2.  
  3.  
  4.  
  5.  
  6.       EXAMPLE PROLOG PROGRAMS
  7.  
  8. these examples are from:
  9.     Logic Programming and Knowledge Engineering by Tore Amble
  10.  
  11. robotplan
  12. goal|brother(abel,Q),PRINT(q=,Q)
  13. parent(adam,cain)
  14. parent(adam,abel)
  15. ancestor(X,Y)|parent(X,Y)
  16. ancestor(X,Z)|parent(Y,Z)ancestor(X,Y)
  17. NOT(ancestor(X,X))
  18. brother(X,Y)|father(F,X)father(F,Y),NOT(eq(X,Y))
  19. father(adam,cain)
  20. father(adam,abel)
  21.  
  22. alpine skier
  23. goal|clubmember(X),climber(X),neg(skier(X)),PRINT(x=,X)
  24. clubmember(tony)
  25. clubmember(mike)
  26. clubmember(john)
  27. climber(X)|clubmember(X),neg(skier(X))
  28. neg(like(X,rain))|climber(X)
  29. neg(skier(X))|neg(likes(X,snow))
  30. neg(likes(mike,X))|likes(tony,X)
  31. likes(mike,X)|neg(likes(tony,X))
  32. likes(tony,rain)
  33. likes(tony,snow)
  34.  
  35. connected
  36. leg(a,d)
  37. leg(a,c)
  38. leg(a,b)
  39. leg(c,d)
  40. leg(d,e)
  41. connect(X,Y)|leg(X,Y)
  42. connect(X,Z)|leg(X,Y),connect(Y,Z)
  43. deriv(x,1)
  44. deriv(N,0)|number(N)
  45. deriv(+(U,V),+(U1,V1))|deriv(U,U1),deriv(V,V1)
  46. deriv(-(U,V),-(U1,V1))|deriv(U,U1),deriv(V,V1)
  47. deriv(*(U,V),+(*(U1,V),*(U,V1)))|deriv(U,U1),deriv(V,V1)
  48. deriv(/(U,V),/(-(*(U1,V),*(U,V1)),*(V,V)))|deriv(U,U1),deriv(V,V1)
  49. deriv(^(U,N),^(*(N,U),-(N,1)))|deriv(U,U1)
  50. deriv(EXP(U),*(EXP(U),U1))|deriv(U,U1)
  51.  
  52. ex1
  53.  
  54. p(X)|q(X),r(X)
  55. p(scrmph)
  56. q(a)
  57. q(b)
  58. q(c)
  59. r(b)
  60. r(c)
  61. s(b)
  62. s(c)
  63. w(U)|s(U)
  64. goal|p(U),w(U)
  65. goal|alter(ego)
  66. goal|DQ(p(X,p(Z,Y)),Z,p(X,Y),Z),lst(X,Y,Z)
  67. DQ(p(p(U,U),U),R(a,b),V,R(A,B))
  68. lst(I,J,K)
  69.  
  70. orphan
  71. father(hans,gerhard)
  72. mother(lise,gerhard)
  73. father(ivan,heinz)
  74. parent(X,Y)|father(X,Y)
  75. parent(X,Y)|mother(X,Y)
  76. hasparent(Y)|parent(X,Y)
  77. child(gerhard)
  78. child(heinz)
  79. child(beate)
  80. status(gerhard,healthy)
  81. status(heinz,sick)
  82. orphan(X)|child(X),noparent(X)
  83. noparent(X)|parent(Y,X),!,fail
  84. noparent(X)
  85.  
  86. construct
  87. cons(X,Y,.(X,Y))
  88. q1|cons(a,b,Z),PRINT(z=,Z)
  89. q2|cons(a,@,Z),PRINT(z=,Z)
  90. MEM(X,.(X,Y))
  91. MEM(X,.(U,V))|MEM(X,V)
  92. g1|MEM(a,.(a,.(b,.(c,@))))
  93. d1|delete(X,.(a,.(b,.(c,@,))),Y),PRINT(x=,X),PRINT(y=,Y)
  94. delete(X,.(X,Y),Y)
  95. delete(X,.(U,V),.(U,W))|delete(X,V,W)
  96.  
  97. NETWORK database
  98. hasa(animal,inhalant)
  99. hasa(animal,bloodtemp)
  100. hasa(animal,food)
  101. hasa(elephant,texture)
  102. has(mammal,inhalant,oxygen)
  103. has(mammal,bloodtemp,warm)
  104. has(elephant,texture,wrinkled)
  105. has(elephant,food,peanuts)
  106. has(elephant,colour,grey)
  107. has(shark,habitat,ocean)
  108. has(shark,colour,grey)
  109. has(X,Attribute,Value)|isa(X,C),has(C,Attribute,Value)
  110. are(mammal,animal)
  111. are(shark,animal)
  112. are(spermwhale,mammal)
  113. are(elephant,mammal)
  114. isa(clyde,elephant)
  115. isa(bonnie,shark)
  116. isa(X,Z2)|are(Z1,Z2),isa(X,Z1)
  117.  
  118. orphan2
  119. orphan(X)|NOT(parent(Y,X))
  120. NOT(X)|X,!,fail
  121. father(hans,gerhard)
  122. mother(lise,gerhard)
  123. father(ivan,heinz)
  124. parent(X,Y)|father(X,Y)
  125. parent(X,Y)|mother(X,Y)
  126. hasparent(Y)|parent(X,Y)
  127.  
  128. quicksort
  129. QS(.(X,Xs))|part(Xs,X,L,B),QS(L,Ls),QS(B,Bs),APPEND(Ls,.(X,Bs),Ys)
  130. QS(@,@)
  131. part(.(X,Xs),Y,.(X,Ls),Bs)|X<=Y,part(Xs,Y,Ls,Bs)
  132. part(.(X,Xs),Y,Ls,.(x,Bs))|X>Y,part(Xs,Y,Ls,Bs)
  133. partition(@,Y,@,@)
  134. APPEND(@,List,List)
  135. APPEND(.(First,Rest),List,.(First,Temp))|APPEND(Rest,List,Temp)
  136.  
  137. reverse
  138. g1|QS(.(2,.(1,.(3,@))),Qs)
  139. g1|reverse(.(a,.(b,.(c,.(d,.(e,.(f,@)))))),Q),PRINT(q=,Q)
  140. g2|reverse(.(a,.(b,.(c,.(d,.(e,.(f,.(g,.(h,.(i,@))))))))),Q),PRINT(q=,Q)
  141. reverse(@,@)
  142. reverse(.(X,Y),Z)|reverse(Y,Y1),APPEND(Y1,.(X,@),Z)
  143. APPEND(@,List,List)
  144. APPEND(.(First,Rest),List,.(First,Temp))|APPEND(Rest,List,Temp)
  145.  
  146. expert SYSTEM
  147. goal|solve((NOT(X,Y),BOX(X),BOX(Y))),PRINT(x=,X),PRINT(y=,Y)
  148. solve(Y)|prove(Y,false,C)
  149. prove((P,Q),U,W)|!,prove(P,U,V),prove(Q,V,W)
  150. prove(NOT(eq(X,Y)),P,C)|==(X,Y),!,fail
  151. prove(NOT(eq(X,Y)),P,P)|NOT(eq(X,Y)),!
  152. prove(NOT(eq(X,Y)),P,OR(==(X,Y),P))|!
  153. prove(X,U,U)|NOT((X,Any)),!,X,NOT(U)
  154. prove(Y,U,V)|(Y,Z),prove(Z,U,V)
  155.  
  156. robotplan
  157. BOX(a)
  158. BOX(b)
  159. BOX(c)
  160. holds(ON(a,b),start)
  161. holds(ON(b,floor),start)
  162. holds(ON(c,floor),start)
  163. holds(CLEAR(a),start)
  164. holds(CLEAR(c),start)
  165. holds(ON(a,b),final)
  166. holds(ON(b,c),final)
  167. holds(ON(c,floor),final)
  168.  
  169.  
  170.  
  171.  
  172.  
  173.